﻿using DevMy.PublicSetUp;
using MDIDemo.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_IntoTxtBase : Form_BaseDialog
    {
        public Form_IntoTxtBase()
        {
            InitializeComponent();
            Ini();
        }
        /// <summary>
        /// 设备编号
        /// </summary>
        public string EquipmentId;
        /// <summary>
        /// 通道编号
        /// </summary>
        public int ChannelNo;
        /// <summary>
        /// 计算方式
        /// </summary>
        public string CalType;
        /// <summary>
        /// 项目编号
        /// </summary>
        public string ProjectId;
        /// <summary>
        /// 选择文件全路径名称
        /// </summary>
        public string FileName;
        private Class_LookUpEdit class_LookUpEdit;
        private DataTable dataTableProject = new DataTable();

        public void Ini()
        {
            base.SetComponent();

            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("projectName", "项目名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "项目编号");

            LookUpEditColumnModel[] lookUpEditColumnModels1 = new LookUpEditColumnModel[2];
            lookUpEditColumnModels1[0] = new LookUpEditColumnModel("equipmentName", "设备名称");
            lookUpEditColumnModels1[1] = new LookUpEditColumnModel("id", "设备编号");

            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "projectName", "id", lookUpEditColumnModels, "项目信息");

            dataTableProject = class_MySqlDataBase.GetDataTable("SELECT id,projectName,projectType FROM vou_project ORDER BY createDate DESC");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "projectName", "id", lookUpEditColumnModels, "项目信息");
            this.lookUpEdit1.Properties.DataSource = dataTableProject;
            if (dataTableProject != null && dataTableProject.Rows.Count > 0)
                this.lookUpEdit1.ItemIndex = 0;
            else
                this.lookUpEdit1.ItemIndex = -1;

            DataTable dataTableEquip = new DataTable();
            dataTableEquip = class_MySqlDataBase.GetDataTable(@"Select id
            , equipmentName
            From vou_equipment ORDER BY sortNo");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit2, "equipmentName", "id", lookUpEditColumnModels1, "设备名称");
            this.lookUpEdit2.Properties.DataSource = dataTableEquip;
            if (dataTableEquip != null && dataTableEquip.Rows.Count > 0)
                this.lookUpEdit2.ItemIndex = 0;
            else
                this.lookUpEdit2.ItemIndex = -1;

            this.Text = "请选择要导入的文本文件";
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            int ProjectIndex = lookUpEdit1.ItemIndex;
            if (ProjectIndex < 0)
            {
                MessageBox.Show("请选择项目！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int EqiupmentIndex = lookUpEdit2.ItemIndex;
            if (EqiupmentIndex < 0)
            {
                MessageBox.Show("请选择设备！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (FileName == null)
            {
                MessageBox.Show("请选择要导入的文本文件！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ProjectId = lookUpEdit1.EditValue.ToString();
            EquipmentId = lookUpEdit2.EditValue.ToString();
            CalType = this.labelControl5.Text;
            ChannelNo = this.radioGroup2.SelectedIndex + 1;
            this.DialogResult = DialogResult.OK;
        }


        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            int index = lookUpEdit1.ItemIndex;
            if (index > -1)
            {
                if (dataTableProject != null && dataTableProject.Rows.Count > 0)
                    this.labelControl5.Text = dataTableProject.Rows[index]["projectType"].ToString();
            }
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "txt files (*.txt)|*.txt";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog.FileName;
                this.buttonEdit1.Text = FileName;
            }
            openFileDialog.Dispose();
        }
    }
}
